﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using ScintillaNet;
using Descriptions;

namespace LPMEDEditor
{
    internal partial class Document : UserControl,ICodeEditor
    {
        
        #region public properties
        internal string DeviceName{get;private set;}
        internal string ModuleName { get; private set; }
        /// <summary>
        /// Имя документа
        /// </summary>
        internal string Name { get; private set; }
        internal string Path { get; private set; }
        public bool IsCompile { get; private set; }
        internal string LPMEDCode { get { return EditControl.Text; } }
        internal bool OnTabPage { get; private set; }
        internal Scintilla ScDoc { get { return EditControl; } }
        private bool LastIsDirty = false;
        #endregion

        public event DirtyChangedHandler DirtyChanged;

        public Document(string Path, DocumentStorage docstg)
		{
            this.Text = System.IO.Path.GetFileName(Path);

            this.ModuleName = docstg.ModuleName;
            this.Path = Path;
            this.Name = System.IO.Path.GetFileName(Path);
            //TODO:
            IsCompile = true;
			      InitializeComponent();

            EditControl.DirtyChanged += new Scintilla.DirtyChangedHandler(ScDoc_DirtyChanged);
            EditControl.ConfigurationManager.Language = "lpmed";
            EditControl.Text = docstg.LPMEDText;
		}

        void ScDoc_DirtyChanged(bool NewStatus)
        {
         if (this.EditControl.IsDirty != LastIsDirty)
          {
            if (DirtyChanged != null)
            {
              DirtyChanged(this, this.EditControl.IsDirty);
              LastIsDirty = this.EditControl.IsDirty;
            }
          }
        }

        private void sciDocument_SavePointReached(object sender, EventArgs e)
        {
            addOrRemoveAsteric();
        }

        private void sciDocument_SavePointLeft(object sender, EventArgs e)
        {
            addOrRemoveAsteric();
        }

        private void addOrRemoveAsteric()
        {
    
            if (this.EditControl.IsDirty)
                {
                    if (!this.Text.EndsWith(" *"))
                        this.Text += " *";
                }
                else
                {
                    if (this.Text.EndsWith(" *"))
                        this.Text = Text.Substring(0, this.Text.Length - 2);
                }
        }


		private bool save(string path)
		{
            //using (FileStream fs = File.Create(path))
            //using (StreamWriter bw = new StreamWriter(fs))
            //bw.Write(EditControl.Text);//, 0, EditControl.RawText.Length - 1); // Omit trailing NULL

            DocumentStorage stgnew = new DocumentStorage { /*DeviceName = DeviceName,*/ ModuleName = ModuleName, LPMEDText = EditControl.Text };
            Utility.SerializeToFile<DocumentStorage>(path, stgnew);

            DirtyChanged(this, false);
            LastIsDirty = false;
            this.EditControl.IsDirty = false;

            //EditControl.IsDirty = false;
			return true;
		}

        #region ICodeEditor Members

        public void Initialize(Control parent)
        {
            this.Dock = DockStyle.Fill;
            this.Parent = parent;
            parent.PerformLayout();
        }

        public ErrorReport Verify()
        {
            //TODO:
            //throw new NotImplementedException();
            return null;
        }

        /*public bool isDirty
        {
            get
            {
                //TODO: isDirty
                return true;
            }
        }*/

        public void SaveAs(string Path)
        {
            save(Path);
        }

        public void Save()
        {
            SaveAs(Path);
        }

        public void SetIsCompile(bool val)
        {
            this.IsCompile = val;
        }

        public void Close()
        {
            OnTabPage = false;
            Visible = false;
        }

        public void OnSelect()
        {

            OnTabPage = true;
            Visible = true;
        }
        public void OnDeselect()
        {
            Visible = false;
        }

        public void ShowError(System.Drawing.Point Point, string Error)
        {
          /*this.ScDoc.Select();
          this.ScDoc.Focus();*/
          this.ScDoc.NativeInterface.GotoLine(Point.Y - 1);
          this.ScDoc.CurrentPos += Point.X - 1;
        }

        #endregion
    }
}
